package com.home.cca.network;

import com.google.gson.Gson;
import com.home.cca.model.Model;
import com.home.cca.model.Order;
import com.home.cca.util.DateUtil;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class QueryOrdersRequest {

    private static int page = 1;
    private static int school = 0;
    private static final String URL = ApiClient.getBaseUrl() + "/SupplierManager/PSOrderMgr/PSOrderPageData";

    public static List<Order> query(int schoolId, String month) throws IOException, ParseException {
        page = 1;
        return query(createParams(schoolId, month));
    }

    public static List<Order> query(int schoolId) throws IOException {
        page = 1;
        school = schoolId;
        return query(createParams());
    }

    public static List<Order> queryMore() throws IOException {
        page++;
        return query(createParams());
    }

    private static List<Order> query(RequestBody requestBody) throws IOException {
        Request request = new Request.Builder()
                .url(URL)
                .post(requestBody)
                .header("X-Requested-With", "XMLHttpRequest")
                .header("docktoken", "")
                .header("Cookie", ApiClient.getCookie())
                .header("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .header("Accept", "*/*")
                .header("Connection", "keep-alive")
                .build();

        Gson gson = new Gson();
        String execute = ApiClient.execute(request);
        Model model = gson.fromJson(execute, Model.class);
        List<Order> orders = new ArrayList<>();
        for (Object row : model.getRows()) {
            Order order = gson.fromJson(gson.toJson(row), Order.class);
            order.setMatList(new ArrayList<>());
            orders.add(order);
        }
        return orders;
    }

    private static RequestBody createParams() {
        FormBody.Builder formBuilder = new FormBody.Builder()
                .add("SubOrderState", "-1")
                .add("Code", "")
                .add("SchoolId", String.valueOf(school))
                .add("CanteenId", "-1")
                .add("BuyDateStart", "2024-11-30")
                .add("BuyDateEnd", new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date()))
                .add("AskDate", "")
                .add("MainOrderState", "-1")
                .add("page", String.valueOf(page))
                .add("rows", "5");
        return formBuilder.build();
    }

    private static RequestBody createParams(int schoolId, String month) throws ParseException {
        FormBody.Builder formBuilder = new FormBody.Builder()
                .add("SubOrderState", "-1")
                .add("Code", "")
                .add("SchoolId", String.valueOf(schoolId))
                .add("CanteenId", "-1")
                .add("BuyDateStart", month + "-01")
                .add("BuyDateEnd", DateUtil.getLastDayOfMonth(month))
                .add("AskDate", "")
                .add("MainOrderState", "-1")
                .add("page", "1")
                .add("rows", "100");
        return formBuilder.build();
    }
}